package cn.innosmart.aq.customize;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhy.autolayout.utils.AutoUtils;

import cn.innosmart.aq.R;

/**
 * Created by David on 2015/7/10.
 */
public class DetailLoadingDialog extends Dialog {

    private Context mContext;

    public DetailLoadingDialog(Context context, int theme) {
        super(context, theme);
        mContext = context;
        init();
    }

    public DetailLoadingDialog(Context context) {
        super(context, R.style.dialog);
        mContext = context;
        init();
    }


    private static final int CHANGE_LOADINGDOT_WHAT = 1;
    private static final int CHANGE_TITLE_WHAT = 2;
    private static final int CHNAGE_TITLE_DELAYMILLIS = 1000;
    private static final  int CLOSE_TIMER=4;
    private static final int MAX_SUFFIX_NUMBER = 3;
    private static final char SUFFIX = '.';

    private ImageView iv_route;
    private TextView tv;
    private TextView tv_point;
    private TextView tv_timer;
    private RotateAnimation mAnim;
    private boolean cancelable = true;

    private Handler handler = new Handler() {
        private int num = 0;

        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case CLOSE_TIMER:
                    tv_timer.setVisibility(View.GONE);
                    timerOver =null;
                    break;
                case CHANGE_LOADINGDOT_WHAT:
                    StringBuilder builder = new StringBuilder();
                    if (num >= MAX_SUFFIX_NUMBER) {
                        num = 0;
                    }
                    num++;
                    for (int i = 0; i < num; i++) {
                        builder.append(SUFFIX);
                    }
                    tv_point.setText(builder.toString());
                    if (isShowing()) {
                        handler.sendEmptyMessageDelayed(CHANGE_LOADINGDOT_WHAT, CHNAGE_TITLE_DELAYMILLIS);
                        if(isTimer){
                            TimeCount--;
                            if(TimeCount==0){
                                timerOver.onTimerOver();
                            }
                            if(TimeCount>0){
                                tv_timer.setText(String.valueOf(TimeCount));
                            }else{
                                dismiss();
                            }

                        }
                    } else {
                        num = 0;
                    }
                    break;
                case CHANGE_TITLE_WHAT:
                    String string = (String) msg.obj;
                    tv.setText(string);
                    break;
            }
        }

        ;
    };
//18762625681
    private Button bt_positive;
    private Button bt_negative;

    private void init() {
        View contentView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_loadingdetail,null);
//                View.inflate(getContext(), R.layout.dialog_loadingdetail, null);
        setContentView(contentView);
        iv_route = (ImageView) findViewById(R.id.iv_route);
        tv = (TextView) findViewById(R.id.tv);
        tv_point = (TextView) findViewById(R.id.tv_point);
        tv_timer = (TextView) findViewById(R.id.tv_timer);
        bt_positive = (Button) findViewById(R.id.bt_positive);
        bt_negative = (Button) findViewById(R.id.bt_negative);
        bt_negative.setOnClickListener(mBtnOnClickListener);
        bt_positive.setOnClickListener(mBtnOnClickListener);
        initAnim();
        isTimer = false;
        timerOver = null;
        //getWindow().setWindowAnimations(R.anim.alpha_in);
    }

    private View.OnClickListener mBtnOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.bt_positive:
                    mPositiveButtonListener.onClick(v);
                    break;
                case R.id.bt_negative:
                    mNegativeButtonListener.onClick(v);
                    break;
            }
        }
    };

    private CharSequence mPositiveButtonText;
    private View.OnClickListener mPositiveButtonListener;
    private View.OnClickListener mNegativeButtonListener;
    private CharSequence mNegativeButtonText;

    public void setPositiveButton(int textId, View.OnClickListener listener) {
        bt_positive.setVisibility(View.VISIBLE);
        mPositiveButtonText = mContext.getText(textId);
        bt_positive.setText(mPositiveButtonText);
        mPositiveButtonListener = listener;
    }

    public void setPositiveButton(CharSequence text, View.OnClickListener listener) {
        bt_positive.setVisibility(View.VISIBLE);
        mPositiveButtonText = text;
        bt_positive.setText(mPositiveButtonText);
        mPositiveButtonListener = listener;
    }

    public void setNegativeButton(int textId, View.OnClickListener listener) {
        bt_negative.setVisibility(View.VISIBLE);
        mNegativeButtonText = mContext.getText(textId);
        bt_negative.setText(mNegativeButtonText);
        mNegativeButtonListener = listener;
    }

    public void setNegativeButton(CharSequence text, View.OnClickListener listener) {
        bt_negative.setVisibility(View.VISIBLE);
        mNegativeButtonText = text;
        bt_negative.setText(mNegativeButtonText);
        mNegativeButtonListener = listener;
    }

    private void initAnim() {
        mAnim = new RotateAnimation(360, 0, Animation.RESTART, 0.5f, Animation.RESTART, 0.5f);
        mAnim.setDuration(2000);
        mAnim.setRepeatCount(Animation.INFINITE);
        mAnim.setRepeatMode(Animation.RESTART);
        mAnim.setStartTime(Animation.START_ON_FIRST_FRAME);
    }

    @Override
    public void show() {
        if (isTimer) {
            tv_timer.setVisibility(View.VISIBLE);
        } else {
            tv_timer.setVisibility(View.GONE);
        }
        TimeCount = 30;
        iv_route.startAnimation(mAnim);
        handler.sendEmptyMessage(CHANGE_LOADINGDOT_WHAT);
        super.show();
    }

    @Override
    public void dismiss() {
        isTimer=false;
        mAnim.cancel();
        super.dismiss();
    }


    @Override
    public void setCancelable(boolean flag) {
        cancelable = flag;
        super.setCancelable(flag);
    }

    @Override
    public void setTitle(CharSequence title) {
        tv.setText(title);
    }

    public void changeTitle(CharSequence title) {
        Message msg = handler.obtainMessage();
        msg.what = CHANGE_TITLE_WHAT;
        msg.obj = title.toString();
        handler.sendMessage(msg);
    }

    public void changeTitle(int titleId) {
        Message msg = handler.obtainMessage();
        msg.what = CHANGE_TITLE_WHAT;
        msg.obj = getContext().getString(titleId);
        handler.sendMessage(msg);
    }

    @Override
    public void setTitle(int titleId) {
        setTitle(getContext().getString(titleId));
    }

    @Override
    public void onBackPressed() {
    }


    private boolean isTimer = false;
    private int TimeCount = 30;
    public static TimerOver timerOver;

    public void setTimerOver(TimerOver callback) {
        isTimer=true;
        tv_timer.setVisibility(View.VISIBLE);
        timerOver = callback;
    }


    public void closeTimer(){
        isTimer=false;
        handler.sendEmptyMessage(CLOSE_TIMER);
    }

    public static interface TimerOver {
        void onTimerOver();
    }
}
